import request from '@/utils/request'
import { API_CONFIG } from '@/api/config'

// 查询购物车列表
export function listCart(query) {
  const params = {
    ...query,
    orgId: parseInt(API_CONFIG.ORG_ID) // 确保orgId作为数字传递
  }
  return request({
    url: '/mobile/cart/list',
    method: 'get',
    params: params
  })
}

// 获取购物车详细信息
export function getCart(cartId) {
  return request({
    url: `/mobile/cart/${cartId}?orgId=${parseInt(API_CONFIG.ORG_ID)}`,
    method: 'get'
  })
}

// 新增购物车
export function addCart(data) {
  const requestData = {
    ...data,
    orgId: parseInt(API_CONFIG.ORG_ID) // 确保orgId作为数字传递
  }
  return request({
    url: '/mobile/cart/add',
    method: 'post',
    data: requestData
  })
}

// 修改购物车
export function updateCart(data) {
  const requestData = {
    ...data,
    orgId: parseInt(API_CONFIG.ORG_ID) // 确保orgId作为数字传递
  }
  return request({
    url: '/mobile/cart/update',
    method: 'put',
    data: requestData
  })
}

// 删除购物车
export function delCart(cartId) {
  return request({
    url: `/mobile/cart/${cartId}?orgId=${parseInt(API_CONFIG.ORG_ID)}`,
    method: 'delete'
  })
}

// 删除多个购物车商品
export function delCarts(cartIds) {
	return request({
		url: `/mobile/cart/batch-delete`,
		method: 'delete',
		data: {
			cartIds: cartIds,
			orgId: parseInt(API_CONFIG.ORG_ID) // 确保orgId作为数字传递
		}
	})
}

// 获取用户购物车列表（移动端）
export function getUserCartList() {
  return request({
    url: '/mobile/cart/list',
    method: 'get',
    params: {
      orgId: parseInt(API_CONFIG.ORG_ID) // 确保orgId作为数字传递
    }
  })
}

// 更新购物车商品数量
export function updateCartQuantity(cartId, quantity, userId) {
  return request({
    url: '/mobile/cart/update',
    method: 'put',
    data: {
      cartId: String(cartId), // 确保cartId作为字符串传递
      quantity: quantity,
      userId: userId,
      orgId: parseInt(API_CONFIG.ORG_ID) // 确保orgId作为数字传递
    }
  })
}

// 更新购物车选中状态
export function updateCartChecked(cartId, checked) {
  return request({
    url: '/mobile/cart/update',
    method: 'put',
    data: {
      cartId: cartId,
      checked: checked,
      orgId: parseInt(API_CONFIG.ORG_ID) // 确保orgId作为数字传递
    }
  })
}

// 清空购物车
export function clearCart() {
  return request({
    url: '/mobile/cart/clear',
    method: 'delete',
    params: {
      orgId: parseInt(API_CONFIG.ORG_ID) // 确保orgId作为数字传递
    }
  })
}